package madixin.t513;

import java.util.LinkedList;
import java.util.Queue;

class Solution {

    public int findBottomLeftValue(TreeNode root) {
        if (root == null) {
            return -1;
        }
        Queue<TreeNode> index = new LinkedList<>();
        index.add(root);
        while (!index.isEmpty()) {
            root = index.poll();
            if (root.right != null) {
                index.add(root.right);
            }
            if (root.left != null) {
                index.add(root.left);
            }
        }
        return root.val;
    }


    public int findBottomLeftValue1(TreeNode root) {
        if (root == null) {
            return -1;
        }
        int btmLeftVale = -1;
        Queue<TreeNode> index = new LinkedList<>();
        index.add(root);
        while (!index.isEmpty()) {
            int curSize = index.size();
            btmLeftVale = index.peek().val;

            for (int i = 0; i < curSize; i++) {
                TreeNode node = index.poll();
                if (node.left != null) {
                    index.add(node.left);
                }
                if (node.right != null) {
                    index.add(node.right);
                }
            }
        }
        return btmLeftVale;
    }
}